module utils {
	export class ScreenManager {
		private static _instance: utils.ScreenManager;
		private _baseContainer: egret.DisplayObjectContainer;
		private shelter:egret.Shape;
		private _curScreen:BaseScreen;
		private prevScreen:BaseScreen;

		public constructor() {
			this.init();
		}

		private init():void
		{
			this.shelter=new egret.Shape();
			this.shelter.graphics.beginFill(0xacacac);
			this.shelter.graphics.drawRect(0,0,CoreData.instance.screenW,CoreData.instance.screenH);
			this.shelter.graphics.endFill();
		}

		public static get instance(): utils.ScreenManager {
			if (!utils.ScreenManager._instance) {
				this._instance = new utils.ScreenManager();
			}
			return this._instance;
		}

		public get baseContainer():egret.DisplayObjectContainer {
			return this._baseContainer;
		}

		public set baseContainer(value: egret.DisplayObjectContainer) {
			this._baseContainer = value;
		}
		//转换场景
		public changeScreen(toScreenName:string,dispose:boolean=true,paramArr=null):void
		{
			let toScreen=this.getScreenByName(toScreenName,paramArr);
			this.shelter.alpha=0;
			utils.CoreData.instance.stage.addChild(this.shelter);
			TweenMax.to(this.shelter,.2,{alpha:1,ease:Cubic.easeIn,onComplete:()=>{
				this._baseContainer.removeChild(this._curScreen);
				if(dispose){
					this._curScreen.dispose();
				}else{
					this.prevScreen=this._curScreen;
				}
				this.curScreen=toScreen;
				this._baseContainer.addChild(this._curScreen);
				TweenMax.to(this.shelter,.65,{alpha:0,ease:Sine.easeOut,onComplete:()=>{
					this.shelter.parent.removeChild(this.shelter);
				}});
			}});
		}
		//淡出转换场景
		public fadeOutChangeScreen(toScreenName:string,paramArr=null):void
		{
			let toScreen=this.getScreenByName(toScreenName,paramArr);
			this._baseContainer.addChild(toScreen);
			this._baseContainer.addChild(this._curScreen);
			TweenMax.to(this._curScreen,.6,{alpha:0,ease:Sine.easeInOut,onComplete:()=>{
				this._baseContainer.removeChild(this._curScreen);
				this._curScreen.dispose();
				this.curScreen=toScreen;
			}});
		}
		//回退场景,仅在收集勋章界面切换使用
		public backScreen():void
		{
			if(!this.prevScreen) return;
			this._baseContainer.removeChild(this._curScreen);
			this._curScreen.dispose();
			this._curScreen=this.prevScreen;
			this.prevScreen=null;
			this._baseContainer.addChild(this._curScreen);
		}

		public set curScreen(value:BaseScreen){
			this._curScreen=value;
		}

		private getScreenByName(screenName:string,paramArr):BaseScreen
		{
			var ScreenClass:any=egret.getDefinitionByName(screenName);
			var screen:BaseScreen;
			if(paramArr) screen=<BaseScreen>(new ScreenClass(...paramArr));
			else screen=<BaseScreen>(new ScreenClass());
			return screen;
		}
	}
}